/**
 * 
 */
package filePackets;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import cmm.ConnectionInformation;

/**
 * @author peleg
 *
 */
public class NewSessionPacket extends FilePacket {

	public NewSessionPacket(byte[] ba) throws IOException, ClassNotFoundException {
		super(ba);
//		ByteArrayInputStream bis = new ByteArrayInputStream(ba);
//		ObjectInputStream ois = new ObjectInputStream(bis);
//		NewSessionPacket obj = (NewSessionPacket)ois.readObject();
//		bis.close();
//		ois.close();
//		this.setRequestedFile(obj.getRequestedFile());		
	}
	public NewSessionPacket() {
		super();
	}
	private static final long serialVersionUID = 7436403198761530154L;
		/* (non-Javadoc)
	 * @see client.FilePacket#getPacketType()
	 */
	@Override
	public PacketType getPacketType() {
		return PacketType.NEW_SESSION;
	}
	// Data fields
	private Integer RequestedFile;
	private ConnectionInformation ClientData;
	
	
	public ConnectionInformation getClientData() {
		return ClientData;
	}
	public void setClientData(ConnectionInformation clientData) {
		ClientData = clientData.clone();
	}
	/**
	 * @return Integer - the requested file number
	 */
	public Integer getRequestedFile() {
		return RequestedFile;
	}
	public void setRequestedFile(Integer requestedFile) {
		RequestedFile = new Integer(requestedFile);
	}
	
	public byte[] toByteArray() throws IOException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(this);
		oos.flush();
		oos.close();
		bos.close();
		byte [] data = bos.toByteArray();
		return data;
	}
	
	public void get_fields_from_obj() {
		setRequestedFile(((NewSessionPacket)obj).getRequestedFile());
		setClientData(((NewSessionPacket)obj).getClientData());		
	}

}
